package com.water.camera.mark;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.App;
import com.water.camera.bean.EditItem;
import com.water.camera.databinding.LayoutWatermark15Binding;
import com.water.camera.util.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Mark15 extends BaseMark {

    LayoutWatermark15Binding binding;

    public Mark15(@NonNull Context context) {
        super(context);
    }

    public Mark15(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Mark15(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(int type) {
        if (type == 0) {
            binding = LayoutWatermark15Binding.inflate(LayoutInflater.from(getContext()));
            addView(binding.getRoot());
        } else {
            binding = LayoutWatermark15Binding.inflate(LayoutInflater.from(getContext()), this, true);
        }
        update();
        if (type == 0) {
            hideView(binding.binding9, binding.binding11, binding.binding13, binding.binding15);
        }
    }

    @Override
    public void update() {
        setText(binding.binding2, "title15", binding.binding1);
        setText(binding.binding3, "name15", binding.name);
        setText(binding.binding4, "danwei15", binding.danwei);
        setText(binding.binding6, "time15", binding.binding5);
        setTime(binding.binding6, null);

        setText(binding.binding8, "didian15", binding.binding7);
        setText(binding.binding10, "haiba15", binding.binding9);
        setText(binding.binding12, "jingwei15", binding.binding11);

        setText(binding.binding14, "weather15", binding.binding13);
        setText(binding.binding16, "beizhu15", binding.binding15);

        super.update();
    }

    @Override
    protected boolean setTime(TextView tv, SimpleDateFormat format) {
        String key = getTimeKey();
        if (TextUtils.isEmpty(key)) {
            String tmp = getClass().getName().substring("com.water.camera.mark.Mark".length());
            key = "time" + tmp + "_time";
        } else {
            key = key + "_time";
        }
        long time = SPHelper.getInstance(App.getApp()).getLong(key, 0);
        if (time > 0) {
            String s1 = new SimpleDateFormat("yyyy.MM.dd").format(time);
            String s2 = Utils.dateToWeek(time);
            String s3 = new SimpleDateFormat("HH:mm").format(time);
            tv.setText(new StringBuffer(s1).append(" ").append(s2).append(" ").append(s3).toString());
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<EditItem> getEditItems() {
        ArrayList<EditItem> list = new ArrayList<>();
        list.add(new EditItem("title15", "标题"));
        list.add(new EditItem("name15", "工程名称"));
        list.add(new EditItem("danwei15", "施工单位"));
        list.add(new EditItem("time15", "时间", true));
        list.add(new EditItem("didian15", "地点"));

        list.add(new EditItem("haiba15", "海拔"));
        list.add(new EditItem("jingwei15", "经纬度"));
        list.add(new EditItem("weather15", "天气"));
        list.add(new EditItem("beizhu15", "备注"));
        return list;
    }

    @Override
    public void onValueChange(EditItem item) {
        if (item.isTime) {
            setTime(binding.binding6, null);
        } else {
            super.onValueChange(item);
        }
    }
}
